package com.gin.pixiv.interceptor

import okhttp3.Interceptor
import okhttp3.Response

/**
 * Token拦截器
 * @author bx002
 * @since 2024/2/18 12:09
 */
class TokenInterceptor(private val token: String?) : Interceptor {
    /**
     * 为POST请求添加token
     */
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val builder = request.newBuilder()
        if (token != null && request.method.uppercase() == "POST") {
            builder.addHeader("x-csrf-token", token)
        }
        return chain.proceed(builder.build())
    }
}